package com.kingbi.system.login.jsfBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.kingbi.system.function.jsfBean.FunctionDataBean;
import com.kingbi.system.login.model.LoginModel;
import com.kingbi.system.login.service.LoginService;
import com.kingbi.util.ConstantFiles.SysMagContant;
import com.kingbi.util.ConstantFiles.UserConstant;

/*
 * 登录类
 */
public class LoginJsfBean extends LoginDataBean {
	private LoginService loginService;

	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		String s_funCode = (String) request.getParameter("funCode");

		this.loginService = loginService;

		if (s_funCode != null && !s_funCode.equals("")
				&& !s_funCode.equals("null")) {
			loadLeftMenu(s_funCode);
		}
	}

	/*
	 * 用户登录验证
	 */
	@SuppressWarnings("unchecked")
	public String UserLoginValidate() {
		Map reqParaMaps = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String userID = (String) reqParaMaps.get("loginForm:userID");
		String password = (String) reqParaMaps.get("loginForm:password");
		try {
			LoginModel loginModel = null;
			if (userID != null && !userID.equals("") && !userID.equals("null")
					&& password != null && !password.equals("") && !password.equals("null")) {
				loginModel = loginService.verifyLoginUser(userID, password);
			} else {
				loginModel = loginService.verifyLoginUser(super.getUserID(),super.getPassword());
			}
			if (loginModel == null) {
				return "failure";
			} else {
				if("0".equals(loginModel.getRoleCount()) || "0".equals(loginModel.getFuncCount())){
					return "authorFailure";
				}
				
				HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
				session.setAttribute(UserConstant.USER_MAP_MODEL_KEY_NAME,loginModel);
				session.setAttribute(UserConstant.USER_MAP_KEY_ID, loginModel.getUserID());
				session.setAttribute(UserConstant.USER_MAP_KEY_NAME, loginModel.getUserName());

				List<FunctionDataBean> funMList = null;
				try {
					funMList = loginService.loadHeadMenu(loginModel.getUserID());
				} catch (Exception e) {
					e.printStackTrace();
				}

				if (funMList.size() > 0) {
					setFunList(funMList);
					session.setAttribute(UserConstant.USER_MAP_HEAD_FUN_LIST_KEY_NAME,funMList);

					FunctionDataBean topMenuModel = new FunctionDataBean();
					topMenuModel = funMList.get(0);
					loadLeftMenu(topMenuModel.getFuncCode());
				}

			}
		} catch (SQLException e) {
			e.printStackTrace();
			return "failure";
		}
		return "login";
	}

	/**
	 * 查询左侧菜单
	 */
	public String loadLeftMenu(String s_funCode) {
		String s_forword = "";
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String s_userID = (String) session.getAttribute(UserConstant.USER_MAP_KEY_ID);

		String frist_page = "nopage";

		if (s_userID != null && !s_userID.equals("")
				&& !s_userID.equals("null")) {
			try {
				List<FunctionDataBean> funList = new ArrayList<FunctionDataBean>();
				List<FunctionDataBean> funErList = null;
				funErList = loginService.loadLeftMenu(s_userID,s_funCode, SysMagContant.FUNCTION_LEVEL_TWO);

				if (funErList != null && funErList.size() > 0) {
					for (int ier = 0; ier < funErList.size(); ier++) {
						FunctionDataBean erMenuModel = new FunctionDataBean();
						erMenuModel = (FunctionDataBean) funErList.get(ier);

						funList.add(erMenuModel);

						if (erMenuModel.getHasChild() != null && erMenuModel.getHasChild().equals("1")) {
							List thrList = null;
							thrList = loginService.loadLeftMenu(s_userID, erMenuModel.getFuncCode(),
									SysMagContant.FUNCTION_LEVEL_THREE);
							if (thrList != null && thrList.size() > 0) {
								for (int ithr = 0; ithr < thrList.size(); ithr++) {
									FunctionDataBean thrMenuModel = new FunctionDataBean();
									thrMenuModel = (FunctionDataBean) thrList.get(ithr);
									funList.add(thrMenuModel);

									if (frist_page != null && frist_page.equals("nopage")) {
										frist_page = thrMenuModel.getPageUrl()+ "&funRight=" + thrMenuModel.getFunRight();
									}
								}
								FunctionDataBean endModel = new FunctionDataBean();
								endModel.setThisTwoMenuEnd("end");
								funList.add(endModel);
							}
						} else {
							if (frist_page != null && frist_page.equals("nopage")) {
								frist_page = erMenuModel.getPageUrl() + "&funRight=" + erMenuModel.getFunRight();
							}
						}
					}
				}

				session.setAttribute(UserConstant.USER_MAP_LEFT_FUN_LIST_KEY_NAME, funList);
				session.setAttribute(UserConstant.USER_MAP_MAIN_FRIST_PAGE_KEY_NAME,frist_page);
				
			} catch (SQLException e) {
				e.printStackTrace();
				return "failure";
			} catch (Exception e) {
				e.printStackTrace();
				return "failure";
			}

			s_forword = "success";
		} else {
			s_forword = "failure";
		}
		return s_forword;
	}

}
